/*
 * Copyright 2004-2005 asoft
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）* 复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.asoft.cn/licenses
 */

package com.asoft.common.base.math;

import java.text.DecimalFormat;

public class NumUtil
{
        // 转换成大写人民币
        public static String  toHighCaseRMB(double eSender)
        {
                double NewMoney;
                int Index, len, flag;
                String StrTemp = null;
                char[] p;
                String Number[]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
                String MonetaryUnit[] = {"万","仟","佰","拾","亿","仟","佰","拾","万","仟","佰","拾","元","角","分"} ;
                NewMoney = Math.abs(eSender);
                DecimalFormat deciformat;
                deciformat=(DecimalFormat)DecimalFormat.getInstance();
                deciformat.applyPattern("#######");
                String m = java.lang.String.valueOf(deciformat.format(NewMoney * 100));
                int i;
                if((i = m.indexOf('.')) != -1)
                        m = m.substring(0, i);
                p = new char[m.length()];
                //p = new char[40];
                m.getChars(0, m.length(), p, 0);
                //len=m.length();
                if (NewMoney > 100000000000.00)
                {
                        StrTemp = ""; // FloatAsComma( NewMoney, False );
                        return StrTemp;
                }
                if (NewMoney < 0.01)
                {
                        StrTemp = "零";
                        return StrTemp;
                }
                if (eSender<0)
                {
                        StrTemp = "负";
                }
                else
                {
                        StrTemp = "";
                }
                flag = 1;
                len =p.length;
                for (Index=(15-len);Index<15;Index++)
                {
                        if (p[Index - 15 + len]!='0')
                        {
                                StrTemp = StrTemp + Number[Integer.parseInt(String.valueOf(p[Index-15+len]))];
                                StrTemp = StrTemp + MonetaryUnit[Index];
                        }
                        else
                        {
                                if (Index == 5)
                                {
                                        if ((p[Index - 14 + len]!= '0') || (p[Index-13+len] != '0'))
                                        {
                                                StrTemp = StrTemp + MonetaryUnit[Index + 3];
                                                flag = 0;
                                        }

                                }
                                else
                                {
                                       if ((Index == 12) ||( (Index == 8) && (flag ==1)) || (Index == 4))
                                       {
                                             StrTemp = StrTemp + MonetaryUnit[Index];
                                       }
                                       if ((p[Index-15 + len] != '0') && (Index != 14))
                                       {
                                                StrTemp = StrTemp +Number[Integer.parseInt(String.valueOf(p[Index-15+len]))];
                                       }
                                }
                        }
                }
                if (p[m.length()-1] == '0')
                {
                        StrTemp = StrTemp + "整";
                }
                return StrTemp;
        }

        // 将数字转换为中文大写
        public static String  num2HighCaseChinese(String sStr)
        {
                String chinese[]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
                String nums[]={"0","1","2","3","4","5","6","7","8","9"};
                for(int  i = 0; i < chinese.length; i ++)
                {
                        sStr = sStr.replaceAll(nums[i],chinese[i]);
                }

                return sStr;
        }
}
